<?php
/**
 * Created by PhpStorm.
 * User: trisatria
 * Date: 1/6/14
 * Time: 3:32 PM
 */

namespace Admin\Model;
use Admin\Model\AdminUser;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class AdminCommendTable{

    protected $tableGateway;
    protected $db;
    public function onDispatch(\Zend\Mvc\MvcEvent $e)
    {

        return parent::onDispatch($e);
    }

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }


    public function fetchAll()
    {
        $select = new Select('comment_topic');
        // create a new result set based on the Album entity
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new AdminCommend());
        // create a new pagination adapter object
        $paginatorAdapter = new DbSelect(
        // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter(),
            // the result set to hydrate
            $resultSetPrototype
        );
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }

    public function getCommend($id)
    {
        $id  = (int) $id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }

    public function getCommendbyTopic($topicid)
    {
        $select = new Select('comment_topic');
        $select->where(array('topic_id'=> $topicid));
        // create a new result set based on the Album entity
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new AdminCommend());
        // create a new pagination adapter object
        $paginatorAdapter = new DbSelect(
        // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter(),
            // the result set to hydrate
            $resultSetPrototype
        );
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }

    public function getCommentTopicById($topicid,$paginated = true){

            if($paginated) {
                $select = new Select('comment_topic');
                $select->where(array('topic_id'=> $topicid));
                // create a new result set based on the Album entity
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new AdminCommend());
                // create a new pagination adapter object
                $paginatorAdapter = new DbSelect(
                // our configured select object
                    $select,
                    // the adapter to run it against
                    $this->tableGateway->getAdapter(),
                    // the result set to hydrate
                    $resultSetPrototype
                );
                $paginator = new Paginator($paginatorAdapter);
                return $paginator;
            }
            $resultSet = $this->tableGateway->select();
            return $resultSet;
    }

    public function count_comment_topic($topic_id){

    }

    public function saveCommend(AdminCommend $commend)
    {
        $data = array(

            'user_id' => $commend->user_id,
            'topic_id' => $commend->topic_id,
            'content' => $commend->content,
            'create_date' => $commend->create_date,
        );

        $id = (int)$commend->id;
        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
            if ($this->getCommend($id)) {
                $this->tableGateway->update($data, array('id' => $id));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
    }

    public function deleteCommend($id)
    {
        $this->tableGateway->delete(array('id' => $id));
    }
}
